package cn.wolfcode.ssm.service.impl;

import cn.wolfcode.ssm.domain.Customer;
import cn.wolfcode.ssm.domain.Employee;
import cn.wolfcode.ssm.mapper.CustomerMapper;
import cn.wolfcode.ssm.qo.QueryObject;
import cn.wolfcode.ssm.service.ICustomerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CustomerServiceImpl implements ICustomerService {

    @Autowired
    private CustomerMapper customerMapper;
    @Override
    public void delete(Long id) {
        customerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(Customer customer) {
        //获取当前登录用户
        Employee employee = (Employee)SecurityUtils.getSubject().getPrincipal();
        //设置录入人为当前登录用户
        customer.setInputUser(employee);
        //设置销售人员当前登录用户
        customer.setSeller(employee);
        //设置录入时间为当前系统时间
        customer.setInputTime(new Date());
        customerMapper.insert(customer);
    }

    @Override
    public Customer getOne(Long id) {
        return customerMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Customer> getAll() {
        return customerMapper.selectAll();
    }

    @Override
    public void update(Customer customer) {
        customerMapper.updateByPrimaryKey(customer);
    }


    @Override
    public PageInfo<Customer> selctForPageResult(QueryObject qo) {
        //使用分页插件,传入当前页,每页显示数量  按照录入时间倒序排序
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(),"input_time desc");
        List<Customer> customers = customerMapper.selectForResult(qo);
        return new PageInfo(customers);
    }

    @Override
    public Customer selectByTel(String tel) {
        return customerMapper.selectByTel(tel);
    }

    @Override
    public void changeStatus(Long id, Integer status) {
        customerMapper.changeStatus(id,status);
    }
}
